<%-- 
    Document   : actor
    Created on : May 1, 2013, 11:54:22 AM
    Author     : phdinusa
--%>

<%@page import="db.UserDAO"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="db.DB"%>
<%@page import="db.MovieObj"%>
<%@page import="db.ActorObj"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <%
        int actorId=Integer.parseInt(request.getParameter("id"));
        ActorObj actorObj=null;
        int numVotes=0;
        
            ArrayList movieIds = null;
            ArrayList movieNames = null;
            ArrayList movieGenres=null;
        
        DB data=new DB();
        Connection conn=null;
        Statement stm=null;
        ResultSet res=null;
        try
        {
            conn=data.getConnection();
            stm=conn.createStatement();
            res=stm.executeQuery("SELECT Name, Age, MF, Rating FROM Actor WHERE Id="+actorId);
            res.next();
            actorObj=new ActorObj(actorId, res.getString(1), res.getString(3).charAt(0), res.getInt(2));
            actorObj.setRating(res.getInt(4));
            
            Statement stm2 = conn.createStatement();
            ResultSet res2 = stm2.executeQuery("SELECT MovieId FROM AppearedIn WHERE ActorId="+actorId);
            
            while(res2.next()){
                actorObj.addAppeared(res2.getInt(1));
            }
            
            movieIds = actorObj.getAppeared();
            movieNames = new ArrayList();
            movieGenres=new ArrayList();
            //Statement stm3 = conn.createStatement();
            for(int i=0;i<movieIds.size();i++){
                ResultSet res3 = stm.executeQuery("SELECT Name, Type FROM Movie WHERE Id="+movieIds.get(i));
                res3.next();
                movieNames.add(res3.getString(1));
                movieGenres.add(res3.getString(2));
            }
            
            pageContext.setAttribute("actor", actorObj);
            
            res=stm.executeQuery("SELECT COUNT(*) FROM ActorRatings WHERE ActorId="+actorId);
            res.next();
            numVotes=res.getInt(1);
        }
        catch (Exception e)
        {
            
        }
        conn=null;
    %>
    <head>
        <title>DataFlix - <%=actorObj.getName()%></title>
	
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<meta http-equiv="content-language" content="en" />
	<meta name="description" content="" />
	<meta name="language" content="en" />
	
	<link rel="shortcut icon" href="http://www.iconarchive.com/download/i59247/franksouza183/fs/Apps-firefox.ico"/>
	
	<link type="text/css" rel="stylesheet" href="./styles/fonts.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/header.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/fancybox.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/images.css"/>

	<link type="text/css" rel="stylesheet" href="./styles/charcoal.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/actorpage.css"/>
	
	<script type="text/javascript">
		function show(divID)
		{
			divID.className='hidden';
			document.getElementById('actHid').className='unhidden';
		}
	</script>
    </head>
    <body>
        
        <jsp:include page="./header.jsp" />
        
        <div id="actInfo">
            <span id="actimg">
		<a class="fancyBox" <%="id=\"actor"+actorObj.getId()+"\""%> <%="style=\"background-image: url(./images/actors/actor"+actorObj.getId()+".jpg\""%>></a>
            </span>
            
            <div id="info">
                <h1>${actor.name}</h1>
                <p id="rating">User rating: <%  for (int v=0; v<actorObj.getRating(); v++)
                                    {%> &#9733; <%}%></span></p>
                <p id="rateStat">Out of <%=numVotes%> votes: ${actor.getRating()} stars</p>
                <p>Gender: ${actor.getGender()}</p>
                <p>Age: ${actor.getAge()}</p>
            
                <% String path=null;   File f=null;    FileInputStream fis=null;   InputStreamReader inStream=null;    BufferedReader buff=null; %>
                <p id="summary">
                    <%
                        path=application.getRealPath("/");
                        f=new File(path+"images/actors/actor"+actorObj.getId()+".txt");
                        fis=new FileInputStream(f);
                        inStream = new InputStreamReader(fis);
                        buff = new BufferedReader(inStream);

                        path=buff.readLine();
                        out.print(path);
                    %>
                </p>
	
	
                <ul class="contentRow">

                    <h2>Movies that ${actor.name} has appeared in</h2>

                    <%
                    int q=0;
                    for (int j=0; j<movieNames.size(); j++)
                    {   
                    %>                       
                    <li <%if (q-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="id=\""+movieGenres.get(j)+"\""%> <%="href=./movie.jsp?id="+movieIds.get(j)%>>
                        <img <%="id=\"movie"+movieIds.get(j) +"\""%> <%="style=\"background-image: url(./images/movies/"+((String)movieGenres.get(j)).toLowerCase()+"/movie"+movieIds.get(j)+".jpg\""%>/>
                        <div class="overlay">
                            <h1><%= movieNames.get(j) %></h1>
                            <div class="shortDesc">
                                <%
                                    path=application.getRealPath("/");
                                    String g=(String)movieGenres.get(j);
                                    f=new File(path+"images/movies/"+g.toLowerCase()+"/movie"+movieIds.get(j)+"Desc.txt");
                                    fis=new FileInputStream(f);
                                    inStream = new InputStreamReader(fis);
                                    buff = new BufferedReader(inStream);

                                    path=buff.readLine();
                                    out.print(path.substring(0, 205)+"...");
                                %>
                            </div>
                        </div>
                    </a></li>
                    <%
                        q++;
                        if (q==5)
                            q=0;
                    }
                %>
                </ul>
                
                <%
                try
                {
                    conn=data.getConnection();
                    stm=conn.createStatement();
                    String checker="SELECT COUNT(*) FROM ActorRatings WHERE CustomerId="+((UserDAO)session.getAttribute("user")).getId()+" AND ActorId="+actorObj.getId();
                    res=stm.executeQuery(checker);
                    res.next();
                    if (res.getInt(1)<1)
                    {
                        %>
                <div id="score">
                    <form action="rater.jsp">
                        <h1>Give this actor a rating?</h1>
                        <select name="score">
                            <option value="" selected="selected">Your rating</option>
                            <option value="1">&#9733;</option>
                            <option value="2">&#9733;&#9733;</option>
                            <option value="3">&#9733;&#9733;&#9733;</option>
                            <option value="4">&#9733;&#9733;&#9733;&#9733;</option>
                            <option value="5">&#9733;&#9733;&#9733;&#9733;&#9733;</option>
                        </select>
                        <input type="hidden" name="actor" value="${actor.getId()}" />
                        <input id="rateSub" name="rateSub" value="Submit rating" type="submit"/>
                    </form>
                </div>
                        <%
                    }
                }
                catch (Exception e)
                {

                }
            %>
                
            </div>
        
        </div>
    </body>
</html>


